package cn.stylefeng.guns.modular.user.controller;

import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.order.service.IOrderService;
import cn.stylefeng.guns.modular.user.model.User;
import cn.stylefeng.guns.modular.user.service.IWXUserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: 杨煜
 * @Date: 2019/12/10 15:47
 * @Description:
 */
@Controller
@RequestMapping("/rest/user")
public class RestUserController extends BaseController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IWXUserService userService;


    //http://192.168.10.8:8083/weixinservice/wx/getxcxopenid?code=123&wxid=123


    /**
     * 验证用户
     *
     * @param code
     * @param wxid
     * @return
     */
    @RequestMapping(value = "/info")
    @ResponseBody
    public ResponseData info(String code, String wxid, String encryptedData, String iv) {

        Map<String, Object> map = new HashMap<>();

        String wxserviceurl = ConstantsContext.getWxserviceurl();
        if (null != wxserviceurl) {
            wxserviceurl += ("?code=" + code + "&wxid=" + wxid + "&encryptedData=" + encryptedData + "&iv=" + iv);
            System.out.println("请求wxservice接口：" + wxserviceurl);

            RestTemplate restTemplate = new RestTemplate();
            JSONObject forObject = restTemplate.getForObject(wxserviceurl, JSONObject.class);
            System.out.println("请求wxservice接口返回值：" + forObject.toJSONString());
            Integer cd = forObject.getInteger("code");

            //{
            //    "code": 0,
            //    "data": {
            //        "watermark": {
            //            "timestamp": 1576134839,
            //            "appid": "wx83c87240664d2547"
            //        },
            //        "nickName": "董大倩",
            //        "avatarUrl": "https://wx.qlogo.cn/mmopen/vi_32/pCUrja4Z78Myy6sfeZCrnH780wjOhwhzYkVfFibVbZ1QB3oEmwkpToVbIvzf69J5DrvUUCeo1MwIXqeibUDjtmnQ/132",
            //        "province": "",
            //        "gender": 0,
            //        "language": "zh_CN",
            //        "country": "",
            //        "city": "",
            //        "openId": "oaTSg4rYIkt27i1pSJ4fmE_-EcLg"
            //    }
            //}

            if(!cd.equals(0)){
                return ResponseData.error("验证失败！");
            }
            JSONObject data = forObject.getJSONObject("data");

            String openId = data.getString("openId");
            User byOpenId = userService.getByOpenId(openId);
            if(null != byOpenId){
                return ResponseData.success(byOpenId);
            }else{
                User user = new User();
                user.setCreateTime(new Date());
                user.setHeadimg(data.getString("avatarUrl"));
                user.setOpenid(openId);
                user.setAddress(data.getString("province"));
                user.setName(data.getString("nickName"));
                user.setGender(data.getInteger("gender"));
                if (userService.save(user)){
                    return ResponseData.success(user);
                }
            }
        }

        return ResponseData.error("验证失败");
    }





}
